// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        prePeople: {
            default: null,
            type: cc.Prefab,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // define
        this.actionUp    = false;
        this.actionDown  = false;
        this.actionLeft  = false;
        this.actionRight = false;

        this.speed = 50;
        this.speedRatio = 1;

        // add key down and key up event
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);

        // load people node
        var scene = cc.director.getScene();
        var people = cc.instantiate(this.prePeople);
        people.parent = this.node;
        people.setPosition(0, -320);
        people.width = 20;
        people.height = 45;
        this.people = people;

        // 悬浮 离开 点击
        people.on('mousemove', function (event) {
            // console.log('mousemove');
        }, this);
        people.on('mouseleave', function (event) {
            // console.log('mouseleave');
        }, this);
        people.on('mousedown', function (event) {
            // console.log('mousedown');
            window.location.href = "VideoClient://";
        }, this);
    },

    onDestroy () {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }, 

    onKeyDown: function (event) {
        var speed = 10;
        switch(event.keyCode) {
            // ctrl
            case cc.macro.KEY.ctrl:
                this.speedRatio = 3;
                break;
            // up down left right                
            case cc.macro.KEY.up:
            case cc.macro.KEY.w:
                this.actionUp = true;
                break;
            case cc.macro.KEY.down:
            case cc.macro.KEY.s:
                this.actionDown = true;
                break;
            case cc.macro.KEY.left:
            case cc.macro.KEY.a:
                this.actionLeft = true;
                break;
            case cc.macro.KEY.right:
            case cc.macro.KEY.d:
                this.actionRight = true;
                break;
        }
    },

    onKeyUp: function (event) {
        switch(event.keyCode) {
            // ctrl
            case cc.macro.KEY.ctrl:
                this.speedRatio = 1;
                break;
            // up down left right      
            case cc.macro.KEY.up:
            case cc.macro.KEY.w:
                this.actionUp = false;
                break;
            case cc.macro.KEY.down:
            case cc.macro.KEY.s:
                this.actionDown = false;
                break;
            case cc.macro.KEY.left:
            case cc.macro.KEY.a:
                this.actionLeft = false;
                break;
            case cc.macro.KEY.right:
            case cc.macro.KEY.d:
                this.actionRight = false;
                break;
        }
    },

    start () {

    },

    update (dt) {
        var pp = this.people.getPosition();
        var dtSpeed = this.speed * this.speedRatio * dt;
        var x = pp.x, y = pp.y;
        if (this.actionUp) {
            y = pp.y + dtSpeed;
        } else if (this.actionDown) {
            y = pp.y - dtSpeed;
        } else if (this.actionLeft) {
            x = pp.x - dtSpeed;
        } else if (this.actionRight) {
            x = pp.x + dtSpeed;
        }
        this.people.setPosition(x, y);
    }

});
